// 1、导入相关的依赖包
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

// 2、定义数据类型
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// 3、解析数据类型对应的具体数据
const resolvers = {
  Query: {
    hello: () => 'Hello Apollo!',
  },
};

// 4、整合apollo-server和express
async function startApolloServer(typeDefs, resolvers) {
  const server = new ApolloServer({ typeDefs, resolvers });
  const app = express();
  await server.start();
  server.applyMiddleware({ app, path: 'api/graphql' });

  // 5、启动监听
  app.listen(9000, () => {
    console.log(
      `Server is running in https://localhost:9000${server.graphqlPath}`
    );
  });
}

startApolloServer(typeDefs, resolvers);
